<template>
  <!--
       1 el-table显示数据,分页 
       2  token cookie|session 服务端保存状态信息方式   
       H5 token 如果有值 登录状态 否则就是登录
       进入某个页面就要进行拦截：判断用户是否登录
       通过什么方式：路由守卫
       3 添加和更新
  -->


  <div class="categorys">
    <div class="category-tools">
      <el-button type="primary" @click="toAdd">添加</el-button>
    </div>
    <el-table :data="areas" style="width: 100%">
      <el-table-column prop="id" label="id" />
      <el-table-column prop="name" label="name" />
      <el-table-column prop="pic" label="pic" />
      <el-table-column prop="showStatus" label="showStatus"/>
      <el-table-column prop="sort" label="sort" />
      <el-table-column prop="subTitle" label="subTitle" />
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="toEdit(scope.row)">更新</el-button>
          <el-button link type="primary" size="small" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination layout="prev, pager, next" :page-size="page.size" :total="page.total"
      @current-change="currentchange" />
  </div>
  <el-dialog v-model="dialogFormVisible">
    <el-form :model="area">
      <el-form-item label="id" :label-width="formLabelWidth">
        <el-input v-model="area.id" autocomplete="off" />
      </el-form-item>
      <el-form-item label="name" :label-width="formLabelWidth">
        <el-input v-model="area.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="pic" :label-width="formLabelWidth">
        <el-input v-model="areas.pic" autocomplete="off" />
      </el-form-item>
      <el-form-item label="showStatus" :label-width="formLabelWidth">
        <el-input v-model="area.showStatus" autocomplete="off" />
      </el-form-item>
      <el-form-item label="sort" :label-width="formLabelWidth">
        <el-input v-model="area.sort" autocomplete="off" />
      </el-form-item>
      <el-form-item label="subTitle" :label-width="formLabelWidth">
        <el-input v-model="area.subTitle" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="save">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script>
import { defineComponent } from "vue"
import { cms_prefrence_areaPage, cms_prefrence_areaDelId, cms_prefrence_areaAdd, cms_prefrence_areaEdit } from "../../http/cms_prefrence_area.js";
import { ElMessage } from 'element-plus'
import { cloneDeep } from 'lodash-es'
export default defineComponent({
  data() {
    return {
      areas: [],
      page: {
        total: 0,
        current: 1,
        size: 10
      },
      dialogFormVisible: false,
      area: {
        "id": 0,
        "name": "",
        "pic": "",
        "showStatus": 0,
        "sort": 0,
        "subTitle": ""
      },
      formLabelWidth: 80,
      flag:true

    }
  },
  mounted() {
    this.getPreFrencesPage(1)
  },
  methods: {
    toEdit(area) {
      console.log(area);
      this.dialogFormVisible = true;
      this.area = cloneDeep(area);
      this.flag = false
    },
    getPreFrencesPage(current) {
      const data = {
        current: current,
        size: 5
      }
      cms_prefrence_areaPage(data).then(res => {
        console.log(res);
        const page = res.data.page;
        this.areas = page.records;
        this.page = page;
      }).catch(err => {
        console.log(err);
      })

    },
    currentchange(current) {
      // console.log(current);
      this.getPreFrencesPage(current);
      this.page.current = current;//数据更新目前显示的页面
    },
    del(id) {
      //删除数据
      //模拟删除，服务器进行处理 
      //要别人反悔机会
      // 如果只有一条数据，删除数据之后如何处理？
      //人机交互
      console.log(id)
      
      cms_prefrence_areaDelId(id).then(res => {
        if (res.success) {
          this.getPreFrencesPage(this.page.current)

        }
        else {
          console.log(res.msg)
          return false
        }


      }).catch(err => {

      })
    },
    toAdd() {
      //到添加的页面
      this.dialogFormVisible = true;
      this.area = {
        'id': "",
        'name': "",
        'icon': "",
        'subject_count': "",
        'show_status': "",
        'sort': ""
      }
      // const newAPI = {
      //       "id": 0,
      //       "name": "",
      //       "pic": "",
      //       "showStatus": 0,
      //       "sort": 0,
      //       "subTitle": "",
      //       "isNew":true
      //     }
      // this.areas.push(newAPI);
    },
    save() {
      const Area = this.area;
      if (this.flag) {
        cms_prefrence_areaAdd(Area).then(res => {
          if (res.success) {
          //   //刷新页面
            this.dialogFormVisible = false;
          //   this.getPreFrencesPage(this.page.current)
            ElMessage(res.msg)
          }
          else {
            
            ElMessage(res.msg)
            console.log(res)
            return false
          }
       
        }).catch(err => {
          ElMessage('网络错误联系管理员')
        })
      }
      else {
        cms_prefrence_areaEdit(Area).then(res => {
          if (res.success) {
            //刷新页面
            this.dialogFormVisible = false;
            this.getPreFrencesPage(this.page.current)
            ElMessage(res.msg)
          }
          else {
            ElMessage(res.msg)
            return false
          }
        }).catch(err => {
          ElMessage('网络错误联系管理员')
        })
      }

    }

  },
});
</script>
<style lang="scss" scoped></style>
